﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using Microsoft.WindowsMobile.PocketOutlook;

namespace SmsLogger
{
    public partial class frmMain : Form
    {
        private bool enableInterceptor = false;

        public frmMain()
        {
            InitializeComponent();
            LoadSmsIntercepter();
        }

        #region SmsInterceptor Region

        MessageInterceptor _smsInterceptor = null;
        private void LoadSmsIntercepter()
        {
            // Default constructor indicates that message handling should occur
            // on the main application thread, and message should be passed
            // to other message interceptors and to the SMS message reader
            _smsInterceptor = new MessageInterceptor(InterceptionAction.Notify);
            // The name is reported in the form of the Contact's File As field
            // Therefore must check for "Smith, John", not "John Smith"
            _smsInterceptor.MessageCondition = new MessageCondition();
                //new MessageCondition(MessageProperty.Subject,
                //MessagePropertyComparisonType.StartsWith, "30days:", true);

            _smsInterceptor.MessageReceived += SmsInterceptor_MessageReceived;
        }

        private StringBuilder documentText = new StringBuilder();
        void SmsInterceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
            if (enableInterceptor == false)
                return;

            // Cast to SmsMessage to access message body
            // Not expecting to receive any non-SMS messages but use "as" to
            // cast to be extra safe
            SmsMessage newMessage = e.Message as SmsMessage;
            if (newMessage != null)
            {
                documentText.AppendFormat(@"<b>Body:</b> {0}<br/>", newMessage.Body);
                documentText.AppendFormat(@"<b>Name:</b> {0}<br/>", newMessage.From.Name);
                documentText.AppendFormat(@"<b>Address:</b> {0}<br/>", newMessage.From.Address);
                documentText.AppendFormat(@"<b>Last Modified:</b> {0}<br/>", newMessage.LastModified);
                documentText.AppendFormat(@"<b>Received:</b> {0}<br/>", newMessage.Received);
                documentText.Append(@"<br/>");

                webBrowser.DocumentText = documentText.ToString();
            }
        }

        private void frmMain_Closed(object sender, EventArgs e)
        {
            if (_smsInterceptor != null)
            {
                // Remove event handler to assure proper registry cleanup
                _smsInterceptor.MessageReceived -= SmsInterceptor_MessageReceived;
                _smsInterceptor.Dispose();
            }
        }

        #endregion

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemStart_Click(object sender, EventArgs e)
        {
            if (enableInterceptor == false)
                menuItemStart.Text = "Stop";
            else
                menuItemStart.Text = "Start";

            enableInterceptor = !enableInterceptor;
        }

        private void menuItemClear_Click(object sender, EventArgs e)
        {
            documentText.Remove(0, documentText.Length);
        }
    }
}